# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do


  desc "Build"
  lane :build do
    setup_env
    install_provisioning_profile(
      path: "fastlane/cer/develop.mobileprovision",
    )
    build_app(
      scheme: "Demo",
      configuration: "Debug",
      skip_package_ipa: true,
      derived_data_path: "DerivedData",
      cloned_source_packages_path: get_cloned_package_path("build"),
      use_system_scm: true,
    )
  end

  desc "Archive Debug"
  lane :debug do
    setup_env
    install_provisioning_profile(
      path: "fastlane/cer/develop.mobileprovision",
    )
    version = get_version_number(xcodeproj: "Demo.xcodeproj")
    build=get_build_number(xcodeproj: "Demo.xcodeproj")
    commit = last_git_commit
    hash = commit[:commit_hash]
    filename = "demo_#{version}_#{build}_debug_#{hash}"
    build_app(
      scheme: "Demo",
      configuration: "Debug",
      export_method: "development",
      output_directory: "BUILD/#{version}",
      output_name: filename,
      include_bitcode: false,
      include_symbols: true,
      derived_data_path: "DerivedData",
      cloned_source_packages_path: get_cloned_package_path("debug"),
      use_system_scm: true,
      xcargs:'DEBUG_INFORMATION_FORMAT="dwarf-with-dsym"',
    )
    # upload_pgyer
    set_export_environment("debug",filename)
    # bump_version
  end

  desc "Archive Release"
  lane :release do
    setup_env
    install_provisioning_profile(
      path: "fastlane/cer/release.mobileprovision",
    )
    version = get_version_number(xcodeproj: "Demo.xcodeproj")
    build = get_build_number(xcodeproj: "Demo.xcodeproj")
    commit = last_git_commit
    hash = commit[:commit_hash]
    filename = "demo_#{version}_#{build}_release_#{hash}"
    build_app(
      scheme: "Demo",
      configuration: "Release",
      export_method: "ad-hoc",
      output_directory: "BUILD/#{version}",
      output_name: filename,
      include_bitcode: false,
      include_symbols: false,
      # derived_data_path: "DerivedData/release",
      cloned_source_packages_path: get_cloned_package_path("release"),
      use_system_scm: true,
    )
    # upload_pgyer
    set_export_environment("release",filename)
    # bump_version
    
  end

  desc "Archive Store"
  lane :store do
    setup_env
    install_provisioning_profile(
      path: "fastlane/cer/store.mobileprovision",
    )
    version = get_version_number(xcodeproj: "Demo.xcodeproj")
    build=get_build_number(xcodeproj: "Demo.xcodeproj")
    commit = last_git_commit
    hash = commit[:commit_hash]
    filename = "demo_#{version}_#{build}_store_#{hash}"
    build_app(
      scheme: "Demo",
      configuration: "AppStore",
      export_options: {
        method: "app-store",
        signingCertificate: "iPhone Distribution: bojian li (39T92HFT75)",
      },
      output_directory: "BUILD/#{version}",
      output_name: filename,
      include_bitcode: false,
      include_symbols: false,
      # derived_data_path: "DerivedData/store",
      cloned_source_packages_path: get_cloned_package_path("store"),
      use_system_scm: true,
    )
    app_store_connect_api_key(
      key_id: "TFG26P5ASG",
      issuer_id: "b0bb77cd-df39-4d7c-a3f5-584e75ba4207",
      key_filepath: "fastlane/AuthKey_TFG26P5ASG.p8",
      in_house: false,
    )
    upload_to_app_store(
      force: true,
    )
    set_export_environment("store",filename)
    bump_version
  end
  


end

def setup_env
  skip_docs
end

def upload_pgyer
  pgyer(api_key: "64afc04184d4e9152e4343ff67edfa27", user_key: "47c5ee2b458af683b3d71db13f185675")
end

def bump_version
  increment_build_number
  commit_version_bump(xcodeproj: "Demo.xcodeproj")
end

def set_export_environment(config,filename)
  version_number = get_version_number(xcodeproj: "Demo.xcodeproj")
  build_number = get_build_number(xcodeproj: "Demo.xcodeproj")
  commit = last_git_commit
  hash = commit[:commit_hash]
  sh("echo \"PACKAGE_VERSION_NUMBER=#{version_number}\" >> package_info.env")
  sh("echo \"PACKAGE_BUILD_NUMBER=#{build_number}\" >> package_info.env")
  sh("echo \"PACKAGE_COMMIT_HASH=#{hash}\" >> package_info.env")
  sh("echo \"CONFIG=#{config}\" >> package_info.env")
  sh("echo \"FILENAME=#{filename}\" >> package_info.env")
end

def get_branch_name
  bname = git_branch
  bname.gsub! "/", "_"
  bname.gsub! ".", "_"
  return bname
end

def get_cloned_package_path(subpath)
  return "#{ENV["HOME"]}/ClonedSourcePackages/Demo/#{subpath}"
end
